package xyz.yuanjilu.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.riversoft.weixin.common.menu.Menu;
import com.riversoft.weixin.common.message.XmlMessageHeader;
import com.riversoft.weixin.common.message.xml.TextXmlMessage;
import com.riversoft.weixin.mp.base.AppSetting;
import com.riversoft.weixin.mp.menu.Menus;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import xyz.yuanjilu.dao.WeixinConfigDao;
import xyz.yuanjilu.entity.WeixinConfigEntity;
import xyz.yuanjilu.plug.MPPlug;
import xyz.yuanjilu.service.WeixinConfigService;
import xyz.yuanjilu.utils.RRException;



@Service("weixinConfigService")
public class WeixinConfigServiceImpl implements WeixinConfigService {
    private static Logger logger = LoggerFactory.getLogger(WeixinConfigServiceImpl.class);
	@Autowired
	private WeixinConfigDao weixinConfigDao;
	@Autowired
    private List<MPPlug> mpPlugs;
	
	@Override
	public WeixinConfigEntity queryObject(Long id){
		return weixinConfigDao.queryObject(id);
	}
	
	@Override
	public List<WeixinConfigEntity> queryList(Map<String, Object> map){
		return weixinConfigDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return weixinConfigDao.queryTotal(map);
	}
	
	@Override
	public void save(WeixinConfigEntity weixinConfig){
		weixinConfig.setCreateTime(new Date());
		weixinConfigDao.save(weixinConfig);
	}
	
	@Override
	public void update(WeixinConfigEntity weixinConfig){
		weixinConfigDao.update(weixinConfig);
	}
	
	@Override
	public void delete(Long id){
		weixinConfigDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Long[] ids){
		weixinConfigDao.deleteBatch(ids);
	}

	@Override
	public WeixinConfigEntity queryObjectByWeixinCode(String weixinCode) {
		Map<String, Object> param=new HashMap<String, Object>();
		param.put("weixinCode", weixinCode);
		return weixinConfigDao.queryObjectByWeixinCode(param);
	}

	@Override
	public Menu getMenus(Long id) {
		AppSetting appSetting = this.getAppSetting(id);
		return Menus.with(appSetting).get();
	}

	@Override
	public AppSetting getAppSetting(Long id) {
		WeixinConfigEntity weixinConfig = this.queryObject(id);
		return this.getAppSetting(weixinConfig);
	}

	@Override
	public AppSetting getAppSetting(String weixinCode) {
		WeixinConfigEntity weixinConfig = this.queryObjectByWeixinCode(weixinCode);
		return this.getAppSetting(weixinConfig);
	}

	@Override
	public AppSetting getAppSetting(WeixinConfigEntity weixinConfig) {
		if (weixinConfig!=null) {
			AppSetting appSetting = new AppSetting(weixinConfig.getWeixinAppid(),weixinConfig.getWeixinAppsecret());
			appSetting.setToken(weixinConfig.getWeixinToken());
			return appSetting;
		}else{
			throw new RRException("微信配置不存在");
		}
	}

	@Override
	public String getWelCome(WeixinConfigEntity weixinConfigEntity) {
		String welComeText="";
		welComeText+=weixinConfigEntity.getWeixinWelcome();
		welComeText+=this.getPlugDescribe(weixinConfigEntity);
		return welComeText;
	}
	@Override
	public String getPlugDescribe(WeixinConfigEntity weixinConfigEntity) {
		String welComeText="";
		List<MPPlug> plugs=this.getMPPlug(weixinConfigEntity);
		for (MPPlug mpPlug : plugs) {
			welComeText+="\n"+mpPlug.getDescribe(weixinConfigEntity);
		}
		return welComeText;
	}
	
	@Override
	public List<MPPlug> getMPPlug(WeixinConfigEntity weixinConfigEntity) {
		List<MPPlug> plugs= new ArrayList<MPPlug>();
		for (MPPlug mpPlug : mpPlugs) {
			if (mpPlug.superWeixinCode(weixinConfigEntity)) {
				plugs.add(mpPlug);
			}
		}
		return plugs;
	}
	/**
	 * 是否支持关键字
	 * @param weixinConfigEntity
	 * @return
	 */
	@Override
	public boolean superXmlMessageHeader(XmlMessageHeader xmlRequest,WeixinConfigEntity weixinConfigEntity) {
		List<MPPlug> plugs= this.getMPPlug(weixinConfigEntity);
		for (MPPlug mpPlug : plugs) {
			if (mpPlug.superXmlMessageHeader(xmlRequest,weixinConfigEntity)) {
				return true;
			}
		}
		return false;
	}
	
	@Override
	public XmlMessageHeader handle(String weixinUserId,XmlMessageHeader xmlRequest,XmlMessageHeader xmlResponse,WeixinConfigEntity weixinConfigEntity) {
		String text="";
		List<MPPlug> plugs=this.getMPPlug(weixinConfigEntity);
		for (MPPlug mpPlug : plugs) {
			try {
				if (mpPlug.superXmlMessageHeader(xmlRequest,weixinConfigEntity)) {
					xmlResponse=mpPlug.handle(weixinUserId, xmlRequest, xmlResponse, weixinConfigEntity);
				}
			} catch (Exception e) {
				TextXmlMessage toMessage=new TextXmlMessage();
		    	toMessage.setToUser(xmlRequest.getFromUser());
				text+=mpPlug.getName(weixinConfigEntity)+"出现异常请重试";
				toMessage.setContent(text);
				xmlResponse=toMessage;
				logger.error(text, e);
			}
		}
		return xmlResponse;
	}
	@Override
	public XmlMessageHeader getErrorMessage(XmlMessageHeader xmlRequest,WeixinConfigEntity weixinConfigEntity) {
		TextXmlMessage toMessage=new TextXmlMessage();
    	toMessage.setToUser(xmlRequest.getFromUser());
		String text="你输入的指令暂不支持,"+this.getPlugDescribe(weixinConfigEntity);
		toMessage.setContent(text);
		return toMessage;
	}
}
